﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Mt.Site.Core.Navigations.Entities;
using Mt.Site.Core.Synchronization.Data;
using System;

namespace Mt.Site.Core.Synchronization.Navigations
{
    public class MenuInfoStore : DomainService,  IMenuInfoStore
    {
        private readonly IRepository<ChannelObject, Guid> _channelObjectRepository;
        private readonly IRepository<MenuConfig, Guid> _menuConfigRepository;

        public MenuInfoStore(
            IRepository<ChannelObject, Guid> channelObjectRepository,
            IRepository<MenuConfig, Guid> menuConfigRepository)
        {
            _channelObjectRepository = channelObjectRepository;
            _menuConfigRepository = menuConfigRepository;
        }

        public Guid CreateChannelObjectSync(ChannelObject fromEntity, SynchorizeBaseStore input, Guid menuId)
        {
            var newEntity = new ChannelObject();
            var id = Guid.NewGuid();
            newEntity.Id = id;

            newEntity.IsActive = fromEntity.IsActive;
            newEntity.Key = fromEntity.Key;
            newEntity.Key2 = fromEntity.Key2;
            newEntity.Target = fromEntity.Target;
            newEntity.Picurl = fromEntity.Picurl;
            newEntity.PicurlOnHover = fromEntity.PicurlOnHover;
            newEntity.IconUrl = fromEntity.IconUrl;
            newEntity.Title = fromEntity.Title;
            newEntity.Description = fromEntity.Description;
            newEntity.Text = fromEntity.Text;
            newEntity.Order = fromEntity.Order;
            newEntity.LinkUrl = fromEntity.LinkUrl;

            newEntity.Str1 = fromEntity.Str1;
            newEntity.Str2 = fromEntity.Str2;
            newEntity.Str3 = fromEntity.Str3;
            newEntity.Str4 = fromEntity.Str4;
            newEntity.Str5 = fromEntity.Str5;
            newEntity.Pic1 = fromEntity.Pic1;
            newEntity.Pic2 = fromEntity.Pic2;
            newEntity.Bool1 = fromEntity.Bool1;
            newEntity.Bool2 = fromEntity.Bool2;
            newEntity.Bool3 = fromEntity.Bool3;
            newEntity.Bool4 = fromEntity.Bool4;

            newEntity.Time1 = fromEntity.Time1;
            newEntity.Time2 = fromEntity.Time2;
            newEntity.Time3 = fromEntity.Time3;
            newEntity.Time4 = fromEntity.Time4;

            newEntity.SourceID = fromEntity.Id;
            newEntity.Language = input.TargetLanguage;
            newEntity.TenantId = input.ToTenantId;
            newEntity.AutoSynchronous = true;

            newEntity.MenuId = menuId;

            _channelObjectRepository.Insert(newEntity);
            return id;
        }

        public Guid UpdateChannelObjectSync(ChannelObject toEntity, ChannelObject fromEntity)
        {
            toEntity.IsActive = fromEntity.IsActive;
            toEntity.Key = fromEntity.Key;
            toEntity.Key2 = fromEntity.Key2;
            toEntity.Target = fromEntity.Target;
            toEntity.Picurl = fromEntity.Picurl;
            toEntity.PicurlOnHover = fromEntity.PicurlOnHover;
            toEntity.IconUrl = fromEntity.IconUrl;
            toEntity.Title = fromEntity.Title;
            toEntity.Description = fromEntity.Description;
            toEntity.Text = fromEntity.Text;
            toEntity.Order = fromEntity.Order;
            toEntity.LinkUrl = fromEntity.LinkUrl;

            toEntity.Str1 = fromEntity.Str1;
            toEntity.Str2 = fromEntity.Str2;
            toEntity.Str3 = fromEntity.Str3;
            toEntity.Str4 = fromEntity.Str4;
            toEntity.Str5 = fromEntity.Str5;
            toEntity.Pic1 = fromEntity.Pic1;
            toEntity.Pic2 = fromEntity.Pic2;
            toEntity.Bool1 = fromEntity.Bool1;
            toEntity.Bool2 = fromEntity.Bool2;
            toEntity.Bool3 = fromEntity.Bool3;
            toEntity.Bool4 = fromEntity.Bool4;

            toEntity.Time1 = fromEntity.Time1;
            toEntity.Time2 = fromEntity.Time2;
            toEntity.Time3 = fromEntity.Time3;
            toEntity.Time4 = fromEntity.Time4;

            toEntity.SourceID = fromEntity.Id;
            toEntity.IsDeleted = fromEntity.IsDeleted;
            _channelObjectRepository.Update(toEntity);
            return toEntity.Id;
        }

        public Guid CreateConfigSync(MenuConfig fromEntity, SynchorizeBaseStore input)
        {
            var newEntity = new MenuConfig();
            var id = Guid.NewGuid();
            newEntity.Id = id;

            newEntity.Type = fromEntity.Type;
            newEntity.ExtensionData = fromEntity.ExtensionData;
            newEntity.TenantId = input.ToTenantId;
            newEntity.AutoSynchronous = true;

            newEntity.MenuId = fromEntity.MenuId;

            _menuConfigRepository.Insert(newEntity);
            return id;
        }

        public Guid UpdateConfigSync(MenuConfig toEntity, MenuConfig fromEntity)
        {
            toEntity.Type = fromEntity.Type;
            toEntity.ExtensionData = fromEntity.ExtensionData;

            toEntity.IsDeleted = fromEntity.IsDeleted;
            _menuConfigRepository.Update(toEntity);
            return toEntity.Id;
        }
    }
}
